#region Imported Namespaces

using System.Drawing;
using System.Windows.Forms;
using Christian.Helle.PocketNews.PocketPC.NewsProtocol;

#endregion

namespace Christian.Helle.PocketNews.PocketPC.Controls
{
    public class NewsListView : OwnerDrawnList<ArticleHeader>
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (isUpdating)
            {
                return;
            }

            ItemHeight = Height/visibleCount;

            using (var gxOff = Graphics.FromImage(offScreen))
            {
                var top = 0;
                var last_item = false;
                SizeF size;
                var rc_height = 0;
                var rc_width = 0;

                for (var position = vScrollBar.Value; position < vScrollBar.Value + DrawCount; position++)
                {
                    if (top > 0)
                    {
                        //last_item = (Height / top) == 1;
                        last_item = Height - 1 < top;
                    }

                    if (position == SelectedIndex)
                    {
                        if (!last_item)
                        {
                            gxOff.FillRectangle(
                                selectedBrush,
                                0,
                                (position == vScrollBar.Value) ? top : top + 1,
                                ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 2),
                                (position == vScrollBar.Value) ? ItemHeight : ItemHeight - 1);
                        }
                        else
                        {
                            gxOff.FillRectangle(
                                selectedBrush,
                                0,
                                top + 1,
                                ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 1),
                                ItemHeight);
                        }
                    }

                    if (!last_item)
                    {
                        gxOff.DrawLine(
                            pen,
                            0,
                            top + ItemHeight,
                            ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 2),
                            top + ItemHeight);
                    }

                    //gxOff.DrawString(
                    //    item.ToString(),
                    //    Font,
                    //    textBrush,
                    //    new RectangleF(
                    //        topleft,
                    //        top + 3,
                    //        ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 5),
                    //        ItemHeight - 3),
                    //    nwFormat);

                    var item = Items[position];
                    size = gxOff.MeasureString(item.From, Font);
                    rc_height = ItemHeight - (int) size.Height - 6;
                    rc_width = ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 5);

                    Brush brush = (position == SelectedIndex) ? selectedTextBrush : textBrush;
                    gxOff.DrawString(
                        item.From,
                        Font,
                        brush,
                        new RectangleF(
                            topleft,
                            top + 3,
                            rc_width,
                            rc_height),
                        nwFormat);

                    gxOff.DrawString(
                        item.Subject,
                        Font,
                        brush,
                        new RectangleF(
                            topleft,
                            top + size.Height + 6,
                            rc_width,
                            rc_height),
                        nwFormat);

                    top += ItemHeight;
                }

                e.Graphics.DrawImage(offScreen, 1, 1);
            }
        }
    }
}